//
//  MyInvoiceViewModel.m
//  GLGW_Shoping
//
//  Created by whx on 2018/12/10.
//  Copyright © 2018年 yang. All rights reserved.
//

#import "MyInvoiceViewModel.h"
#import "MyInvoiceTableViewCell.h"
#import "LoginModel.h"

@interface MyInvoiceViewModel ()<UITableViewDelegate, UITableViewDataSource>
{
    UITableView *selfTableView;
    MyInvoiceModel *model;
}

@end

@implementation MyInvoiceViewModel

- (instancetype)initWithViewToViewModel:(UITableView *)tableView theSuperView:(UIView *)superView
{
    if (self = [super initWithViewToViewModel:tableView theSuperView:superView]) {
        
        tableView.height -= kHeightProportion_In_iPhoneTriploid(44);
        tableView.delegate = self;
        tableView.dataSource = self;
        tableView.backgroundColor = [UIColor colorWithHexString:@"#F2F2F2"];
        [superView addSubview:tableView];
        selfTableView = tableView;
        model = [MyInvoiceModel new];
        [self requestMyInvoiceListData];
    }
    return self;
}

#pragma mark - tableViewDelegate
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return model.data.infos.count;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    return kHeightProportion_In_iPhoneTriploid(109);
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    MyInvoiceTableViewCell *cell = [MyInvoiceTableViewCell cellWithTableView:tableView];
    cell.type = self.type;
    if (model.data.infos.count > 0) {
        cell.deModel = model.data.infos[indexPath.row];
    }
    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (self.BlockWithDrawlDetail) {
       MyInvoiceListDetailModel *deModel = model.data.infos[indexPath.row];
        self.BlockWithDrawlDetail(deModel);
    }
}


#pragma mark - 数据请求
- (void)requestMyInvoiceListData
{
    if (self.userInforData.length == 0) return;
    @weakify(self);
    _invoiceCommand = [[RACCommand alloc] initWithSignalBlock:^RACSignal * _Nonnull(id  _Nullable input) {
        @strongify(self);
        RACSignal *signal = [RACSignal createSignal:^RACDisposable * _Nullable(id<RACSubscriber>  _Nonnull subscriber) {
            LoginModel *modles = [NSKeyedUnarchiver unarchiveObjectWithData:self.userInforData];
            NSMutableDictionary * param = (NSMutableDictionary *)input;
            param[@"userId"] = modles.data.userId;
            if (self.type == MyInvoiceType_WaitingFor) {  // 全部的列表：不传此字段
                param[@"status"] = @(1);           // 开票状态   1 待开票 2 已开票',
            } else if (self.type == MyInvoiceType_Haved) {
                param[@"status"] = @(2);           // 开票状态   1 待开票 2 已开票',
            }
            [GLGWRequestTool POST:[NSString stringWithFormat:@"%@%@",kApiPrefix,kMyInvoiceListData] lodingView:nil lodingTextString:@"" isLoding:YES parameters:param success:^(id  _Nullable response) {
                [subscriber sendNext:response];
                [subscriber sendCompleted];
            } failure:^(NSError * _Nullable error) {
                [subscriber sendNext:error];
                [subscriber sendCompleted];
            }];
            return nil;
        }];
        return [signal map:^id _Nullable(id  _Nullable value) {
            self->model = [MyInvoiceModel yy_modelWithJSON:value];
            [self makeInvoiceData];
            return self->model;
        }];
    }];
}

- (void)makeInvoiceData
{
    if (model.code == 0) {
        // 有数据
    }
}
@end
